package com.briup.jd2407cms.web.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.briup.jd2407cms.domain.Category;
import com.briup.jd2407cms.domain.extend.CategoryExtend;
import com.briup.jd2407cms.exception.ServiceException;
import com.briup.jd2407cms.result.Result;
import com.briup.jd2407cms.result.ResultCode;
import com.briup.jd2407cms.service.CategoryService;
import com.briup.jd2407cms.utils.CategoryParentIdConverter;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @BelongsProject: cms2
 * @BelongsPackage: com.briup.jd2407cms.web.controller
 * @Author: wangjie
 * @CreateTime: 2024-11-21  10:10
 * @Description: TODO
 * @Version: 1.0
 */
@Api(tags = "栏目模块")
@RestController
@RequestMapping("/auth/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @ApiOperation("分页条件查询")
    @GetMapping("/query")
    private Result query(@RequestParam Integer pageNum,
                         @RequestParam Integer pageSize,
                         Integer parentId){
        IPage<Category> categoryIPage = categoryService.queryByPageAndCondition(pageNum, pageSize, parentId);
        return Result.success(categoryIPage);
    }
    @ApiOperation("查询父栏目")
    @GetMapping("/queryAllParent")
    private Result queryAllParent(){
        List<CategoryExtend> categories = categoryService.queryAllParent();
        return Result.success(categories);
    }
    @ApiOperation("添加栏目")
    @PostMapping("/save")
    private Result add(@RequestBody Category category){
        categoryService.add(category);
        return Result.success();
    }
    @ApiOperation("获取所有父栏目")
    @GetMapping("queryAllOneLevel")
    private Result queryAllOneLevel(){
        List<Category> list = categoryService.queryAllOneLevel();
        return Result.success(list);
    }
    @ApiOperation("根据id获取栏目")
    @GetMapping("/queryById/{id}")
    private Result queryById(@PathVariable Integer id){
        Category category = categoryService.queryById(id);
        return Result.success(category);
    }
    @ApiOperation("更新栏目")
    @PutMapping("/update")
    private Result update(@RequestBody Category category){
        categoryService.edit(category);
        return Result.success();
    }
    @ApiOperation("根据id删除栏目")
    @DeleteMapping("/deleteById/{id}")
    private Result deleteById(@PathVariable Integer id){
        categoryService.deleteById(id);
        return Result.success();
    }
    @ApiOperation("批量删除")
    @DeleteMapping("/deleteByBatch/{ids}")
    private Result deleteByIdAll(@PathVariable List<Integer> ids){
        categoryService.deleteByIdAll(ids);
        return Result.success();
    }
    @ApiOperation("Excel导入栏目数据")
    @PostMapping("/import")
    private Result excelImport(@RequestPart MultipartFile file){
        categoryService.InsertInBatch(file);
        return Result.success("数据导入成功");
    }
    @ApiOperation("将信息导出到excel文件中")
    @GetMapping(value = "/export",produces = "application/octet-stream;charset=utf-8")
    private Result excelExport(HttpServletResponse response){
        categoryService.export(response);
        return Result.success();
    }
}
